// installer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

#define SERVICE_NAME "rinstallerClient"

void HandleError ( LPCSTR str, DWORD error )
{
	cout << str << endl;
}

void install( const char *file )
{
   SC_HANDLE myService, scm;
   cout << "Installing rinstaller Service...\n";
   // open a connection to the SCM
   scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
   if (!scm)
   {
      HandleError("OpenSCManager fails!", GetLastError());
   }
   cout << "Opened Service Control Manager...\n";
   // Install the new service
   myService = CreateService(
      scm, SERVICE_NAME, // the internal service name used by the SCM
      "RInstaller Client",  // the external label seen in the Service Control applet
      SERVICE_ALL_ACCESS,  // We want full access to control the service
      SERVICE_WIN32_OWN_PROCESS,  // The service is a single app and not a driver
      SERVICE_AUTO_START,  // The service will be started by us manually
      SERVICE_ERROR_NORMAL,  // If error during service start, don't misbehave.
      file,
      0, 0, 0, 0, 0);
   if (!myService)
   {
      HandleError("CreateService fails!", GetLastError());
   }
   else
   {
      cout << "Service successfully installed.\n";
   }
   // clean up
   CloseServiceHandle(myService);
   CloseServiceHandle(scm);
}

void uninstall()
{
   SC_HANDLE myService, scm;
   BOOL success;
   SERVICE_STATUS status;
   cout << "Removing Service...\n";
   // Open a Service Control Manager connection
   scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
   if (!scm)
   {
      HandleError("OpenSCManager Fails!", GetLastError());
   }
   cout << "Opened Service Control Manager...\n";
   // Get the service's handle
   myService = OpenService(scm, SERVICE_NAME, SERVICE_ALL_ACCESS | DELETE);
   if (!myService)
   {
      HandleError("OpenService Fails!", GetLastError());
   }
   // Stop the service if necessary
   success = QueryServiceStatus(myService, &status);
   if (!success)
   {
      HandleError("QueryServiceStatus fails!", GetLastError());
   }
   if (status.dwCurrentState != SERVICE_STOPPED)
   {
      cout << "Service currently active.  Stopping service...\n";
      success = ControlService(myService, SERVICE_CONTROL_STOP, &status);
      if (!success)
      {
         HandleError("ControlService fails to stop service!", GetLastError());
      }
      Sleep(500);
   }
   // Remove the service
   success = DeleteService(myService);
   if (success)
   {
      cout << "Service successfully removed.\n";
   }
   else
   {
      HandleError("DeleteService Fails!", GetLastError());
   }
   CloseServiceHandle(myService);
   CloseServiceHandle(scm);
}


void main(int argc, char *argv[])
{
string filename;
	if ( argc < 2 ) {
		cout << "usa: -i (install) <file> o -u (uninstall)" << endl;
	} else {
		if ( strcmp ( argv[1], "-i" ) == 0 ) {
			if ( argc < 3 ) {
				cout << "non hai specificato il servizio" << endl;
			} else {
				for ( int i = 2; i < argc; i++ ) {
					filename.append ( argv[i] );
					if ( i+1 < argc ) {
						filename.append ( " " );	
					}
				}
				cout << "installo " << filename << endl;
				install( filename.c_str() );
			}
		} else if ( strcmp ( argv[1], "-u" ) == 0 ) {
			uninstall();
		} else {
			cout << "parametri non validi" << endl;
		}
	}

}

